package com.zzk.audiovideodemo.surfaceview.video

import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.SurfaceHolder
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.LogUtils
import com.zzk.audiovideodemo.R
import kotlinx.android.synthetic.main.activity_surface_video.*


/**
 * SurfaceView使用示例
 * @author king
 * @time 2020-3-17 14:48
 */
class SurfaceVideoActivity : AppCompatActivity(), SurfaceHolder.Callback {

    //媒体播放控制器
    private var mediaPlayer: MediaPlayer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_surface_video)
        LogUtils.i("SurfaceVideoActivity->onCreate")

        // 把输送给surfaceView的视频画面，直接显示到屏幕上,不要维持它自身的缓冲区
        sv_video.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
        sv_video.holder.setKeepScreenOn(true)
        sv_video.holder.addCallback(this)
        initMedia()
        btn_play.setOnClickListener {
            if (mediaPlayer != null && mediaPlayer!!.isPlaying) {
                pauseVideo()
            } else {
                startVideo()
            }
        }
    }

    override fun onStart() {
        super.onStart()
        LogUtils.i("SurfaceVideoActivity->onStart")
    }

    override fun onRestart() {
        super.onRestart()
        LogUtils.i("SurfaceVideoActivity->onRestart")
    }

    override fun onResume() {
        super.onResume()
        LogUtils.i("SurfaceVideoActivity->onResume")
    }

    override fun onPause() {
        super.onPause()
        LogUtils.i("SurfaceVideoActivity->onPause")
    }

    override fun onStop() {
        super.onStop()
        LogUtils.i("SurfaceVideoActivity->onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        LogUtils.i("SurfaceVideoActivity->onDestroy")
        mediaPlayer?.release()
        mediaPlayer = null
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        LogUtils.i("SurfaceVideoActivity->surfaceCreated")
        mediaPlayer?.setDisplay(holder)//设置播放的容器
        mediaPlayer?.prepareAsync()
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        LogUtils.i("SurfaceVideoActivity->surfaceChanged")
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        LogUtils.i("SurfaceVideoActivity->surfaceDestroyed")
        if (mediaPlayer != null && (mediaPlayer?.isPlaying == true)) {
            mediaPlayer?.stop()
        }
    }

    private fun startVideo() {
        btn_play.text = "暂停"
        mediaPlayer?.start()
    }

    private fun pauseVideo() {
        btn_play.text = "播放"
        mediaPlayer?.pause()
    }

    /**
     * 初始化媒体播放
     */
    private fun initMedia() {
        try {
            mediaPlayer = MediaPlayer()
            //获得播放源访问入口
            val afd = resources.openRawResourceFd(R.raw.video_test) // 注意这里的区别
            //给MediaPlayer设置播放源
            mediaPlayer?.setDataSource(afd.fileDescriptor, afd.startOffset, afd.length)
            mediaPlayer?.setOnPreparedListener {
                startVideo()//缓冲完，播放
            }
            mediaPlayer?.setOnCompletionListener {
                Toast.makeText(this, "播放完毕", Toast.LENGTH_SHORT).show()
                btn_play.text = "重新播放"
            }
        } catch (e: Exception) {
            Log.e("Test", "出错了", e)
        }
    }

}
